﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HandBook.Data;
using Telerik.Web.UI;

public partial class Admin_UserManagement : BasePage
{
    public bool isDuplicate = false;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void uxUser_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        var userList = from user in HandBookData.EatDrinkDB.Users
                       select user;
        uxUser.DataSource = userList;
    }
    protected void uxUser_InsertCommand(object source, GridCommandEventArgs e)
    {
        GridEditableItem insertItem = e.Item as GridEditableItem;
        User user = new User();

        string username = (e.Item.FindControl("txtUserName") as TextBox).Text.Trim();

        //kiểm tra xem styleName đã có ở csdl hay chưa
        var userList = from u in HandBookData.EatDrinkDB.Users
                        select u;
        foreach (var item in userList)
        {
            if (item.UserName.Equals(username))
            {
                isDuplicate = true;
                break;
            }
        }

        if (isDuplicate)
        {
            Response.Write("<script>return false;</script>");
        }
        else
        {
            user.UserName = username;
            user.Password = (e.Item.FindControl("txtPassword") as TextBox).Text.Trim();
            user.Address = (e.Item.FindControl("txtAddress") as TextBox).Text.Trim();
            user.Email = (e.Item.FindControl("txtEmail") as TextBox).Text.Trim();
            user.PhoneNumber = (e.Item.FindControl("txtPhone") as TextBox).Text.Trim();
            user.UserRole = Convert.ToBoolean((e.Item.FindControl("ddlIsAdmin") as DropDownList).SelectedValue.ToString());
            user.IsActivated = Convert.ToBoolean((e.Item.FindControl("ddlIsActivated") as DropDownList).SelectedValue.ToString());

            HandBookData.EatDrinkDB.Users.InsertOnSubmit(user);
            HandBookData.EatDrinkDB.SubmitChanges();
        }
    }
    protected void uxUser_UpdateCommand(object source, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        int userID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["UserID"]);

        var user = HandBookData.EatDrinkDB.Users.Single(u => u.UserID == userID);
        user.Password = (e.Item.FindControl("txtPassword") as TextBox).Text.Trim();
        user.Address = (e.Item.FindControl("txtAddress") as TextBox).Text.Trim();
        user.Email = (e.Item.FindControl("txtEmail") as TextBox).Text.Trim();
        user.PhoneNumber = (e.Item.FindControl("txtPhone") as TextBox).Text.Trim();
        user.UserRole = Convert.ToBoolean((e.Item.FindControl("ddlIsAdmin") as DropDownList).SelectedValue.ToString());
        user.IsActivated = Convert.ToBoolean((e.Item.FindControl("ddlIsActivated") as DropDownList).SelectedValue.ToString());

        HandBookData.EatDrinkDB.SubmitChanges();
    }
    protected void uxUser_DeleteCommand(object source, GridCommandEventArgs e)
    {
        int userID = (int)(e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["UserID"];
        var user = HandBookData.EatDrinkDB.Users.Single(u => u.UserID == userID);

        user.IsActivated = false;
        HandBookData.EatDrinkDB.SubmitChanges();
    }
}